<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>俄罗斯方块</title>
    <style>
        canvas { border: 2px solid #333; }
        #score { font-size: 24px; margin: 10px; }
    </style>
</head>
<body>
    <div id="score">得分: 0</div>
    <canvas id="game" width="300" height="600"></canvas>

<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const BLOCK_SIZE = 30;
const COLS = 10;
const ROWS = 20;
let score = 0;

// 方块形状和颜色
const SHAPES = [
    [[1,1,1,1]], // I
    [[1,1,1],[0,1,0]], // T
    [[1,1,1],[1,0,0]], // L
    [[1,1,1],[0,0,1]], // J
    [[1,1],[1,1]], // O
    [[1,1,0],[0,1,1]], // S
    [[0,1,1],[1,1,0]]  // Z
];

const COLORS = ['#00f0f0', '#f0a000', '#f0f000', '#a000f0', 
               '#f00000', '#00f000', '#0000f0'];

let board = Array(ROWS).fill().map(() => Array(COLS).fill(0));
let currentPiece = null;
let gameOver = false;

function createPiece() {
    const type = Math.floor(Math.random() * SHAPES.length);
    return {
        shape: SHAPES[type],
        color: COLORS[type],
        x: Math.floor(COLS/2) - Math.floor(SHAPES[type][0].length/2),
        y: 0
    };
}

function drawBlock(x, y, color) {
    ctx.fillStyle = color;
    ctx.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE-1, BLOCK_SIZE-1);
}

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    
    // 绘制棋盘
    for (let y = 0; y < ROWS; y++) {
        for (let x = 0; x < COLS; x++) {
            if (board[y][x]) {
                drawBlock(x, y, board[y][x]);
            }
        }
    }
    
    // 绘制当前方块
    if (currentPiece) {
        currentPiece.shape.forEach((row, dy) => {
            row.forEach((value, dx) => {
                if (value) {
                    drawBlock(currentPiece.x + dx, currentPiece.y + dy, currentPiece.color);
                }
            });
        });
    }
}

function collision() {
    return currentPiece.shape.some((row, dy) => {
        return row.some((value, dx) => {
            let newX = currentPiece.x + dx;
            let newY = currentPiece.y + dy;
            return value && (newX < 0 || newX >= COLS || newY >= ROWS || board[newY][newX]);
        });
    });
}

function merge() {
    currentPiece.shape.forEach((row, dy) => {
        row.forEach((value, dx) => {
            if (value) {
                board[currentPiece.y + dy][currentPiece.x + dx] = currentPiece.color;
            }
        });
    });
}

function rotate() {
    const shape = currentPiece.shape;
    const newShape = shape[0].map((_, i) => 
        shape.map(row => row[i]).reverse()
    );
    const oldShape = currentPiece.shape;
    currentPiece.shape = newShape;
    if (collision()) {
        currentPiece.shape = oldShape;
    }
}

function clearLines() {
    let linesCleared = 0;
    for (let y = ROWS - 1; y >= 0; y--) {
        if (board[y].every(cell => cell)) {
            board.splice(y, 1);
            board.unshift(Array(COLS).fill(0));
            linesCleared++;
            y++;
        }
    }
    if (linesCleared) {
        score += linesCleared * 100;
        document.getElementById('score').textContent = `得分: ${score}`;
    }
}

function update() {
    if (gameOver) return;
    
    if (!currentPiece) {
        currentPiece = createPiece();
        if (collision()) {
            gameOver = true;
            alert(`游戏结束！得分: ${score}`);
            return;
        }
    }
    
    currentPiece.y++;
    if (collision()) {
        currentPiece.y--;
        merge();
        clearLines();
        currentPiece = null;
    }
    
    draw();
}

document.addEventListener('keydown', event => {
    if (gameOver) return;
    
    switch(event.keyCode) {
        case 37: // 左
            currentPiece.x--;
            if (collision()) currentPiece.x++;
            break;
        case 39: // 右
            currentPiece.x++;
            if (collision()) currentPiece.x--;
            break;
        case 40: // 下
            currentPiece.y++;
            if (collision()) {
                currentPiece.y--;
                merge();
                clearLines();
                currentPiece = null;
            }
            break;
        case 38: // 上
            rotate();
            break;
    }
    draw();
});

// 重新开始游戏
document.addEventListener('keypress', event => {
    if (event.key === 'r' || event.key === 'R') {
        board = Array(ROWS).fill().map(() => Array(COLS).fill(0));
        score = 0;
        gameOver = false;
        currentPiece = null;
        document.getElementById('score').textContent = `得分: 0`;
        draw();
    }
});

setInterval(update, 1000);
</script>
</body>
</html>